package net.wantongtech.xa.uploadbasicinfo.service;

import com.google.common.io.Files;
import net.wantongtech.xa.uploadbasicinfo.dao.UploadBasicInfoDao;
import net.wantongtech.xa.uploadbasicinfo.vo.Organization;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

/**
 * 创建更新脚本
 *
 * @author MaShibo
 * 日期 :  2019/12/30
 */
@Service
public class CreateUpdateScript {

    @Resource
    private UploadBasicInfoDao uploadBasicInfoDao;

    @SuppressWarnings("UnstableApiUsage")
    public void create() {
        List<Organization> organizationList = uploadBasicInfoDao.queryAllOrganization();
        StringBuilder sql = new StringBuilder();
        int rowNum = 1;
        for (Organization organization : organizationList) {
            String channelId = organization.getChannelId();
            Integer agencyType = organization.getAgencyType();
            sql.append(String.format("UPDATE jg_organization a\n" +
                            "   SET a.channelid = %s, a.agencytype = %s\n" +
                            " WHERE a.orgid = '%s';%n", channelId == null ? "NULL" : String.format("'%s'", channelId),
                    agencyType == null ? "NULL" : agencyType, organization.getOrgId()));
            if (rowNum % 100 == 0) {
                sql.append("commit;").append("\n");
            }
            rowNum++;
        }
        try {
            Files.write(sql.toString(), new File("C:\\Users\\MaShibo\\Desktop\\tmp\\update-org.sql"), Charset.forName("UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
